﻿using JV.Com.Business;
using JV.Com.Internals;
using JV.Com.Model;
using JV.Com.ViewModels.Wrapper;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JV.Com.ViewModels
{
    public sealed class DetailGamePageViewModel : PageBaseViewModel
    {
        private Game m_game;
        private readonly ObservableCollection<GameArticleWrapper> m_articles;
        private readonly ObservableCollection<GameMetadataWrapper> m_metadata;
        public DetailGamePageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            m_game = new Game();
            m_articles = new ObservableCollection<GameArticleWrapper>();
            m_metadata = new ObservableCollection<GameMetadataWrapper>();
            InLoading = true;
        }

        public async override void OnNavigatedTo(object navigationParameter, Windows.UI.Xaml.Navigation.NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            var gameId = (int)navigationParameter;

            m_game = await Service.GetGameAsync(gameId);

            OnPropertyChanged("LargeImageUrl");
            OnPropertyChanged("PageTitle");

            foreach(var metadata in GameMetadataWrapper.GetMetadata(this, m_game))
            {
                m_metadata.Add(metadata);
            }

            await Service.FillGameDetailsAsync(m_game);

            foreach(var article in m_game.Articles)
            {
                var wrapper = GameArticleWrapper.Create(this, article);
                m_articles.Add(wrapper);
            }

            await Service.FillGameScreenshotsAsync(m_game);

            OnPropertyChanged("LargeScreenshots");

            InLoading = false;
        }

        public ObservableCollection<GameArticleWrapper> Articles { get { return m_articles; } }

        public override string PageTitle
        {
            get { return m_game.Title; }
        }

        public string LargeImageUrl
        {
            get { return m_game.LargeImageUrl; }
        }

        public ObservableCollection<GameMetadataWrapper> Metadatas { get { return m_metadata; } }
        public List<string> LargeScreenshots { get { return m_game.LargeScreenshots.Values.ToList(); } }
    }
}
